#include "front/front_interface.h"

using namespace std;
using namespace ppspeech;

int FrontEngineInterface::MergeforModify(vector<pair<string, string>>& seg_word_type, vector<pair<string, string>>& modify_seg_word_type)
{
	vector<string> seg_result;
	GetSegResult(seg_word_type, &seg_result);
	cout << "INFO: Frontend: 合并之前的切分: ";
	for(string seg: seg_result) {
		cout << seg << "／";
	}
	cout << endl;

	FrontMergeEnglishThrow1(seg_word_type);     //合并英语中的'
	FrontMergeEnglishThrow2(seg_word_type);     //合并英语中的's

	vector<pair<string, string>> tmp;
	tmp = MergeBu(seg_word_type);
	modify_seg_word_type = tmp;
	tmp = Mergeyi(modify_seg_word_type);
	modify_seg_word_type = tmp;
	tmp = MergeReduplication(modify_seg_word_type);
	modify_seg_word_type = tmp;
	tmp = MergeThreeTones(modify_seg_word_type);
	modify_seg_word_type = tmp;
	tmp = MergeThreeTones2(modify_seg_word_type);
	modify_seg_word_type = tmp;
	tmp = MergeEr(modify_seg_word_type);
	modify_seg_word_type = tmp;
	seg_result = {};

	GetSegResult(modify_seg_word_type, &seg_result);
	cout << "INFO: Frontend: 合并之后的切分: ";
	for(string seg: seg_result) {
		cout << seg << "／";
	}
	cout << endl;

	return 0;
}

const vector<string> mcvEnThrowMerge = {
	"a's", "ain't", "am's", "aren't", "b'gosh", "ba'ath", "c'est", "c'mon", "can't", "could've",
	"couldn't", "daren't", "didn't", "do's", "doesn't", "don's", "don't", "don'ts", "everybody'd", "hadn't",
	"hasn't", "haven't", "he'd", "he'll", "he's", "her's", "here's", "how'd", "how're", "how's",
	"how've", "i'd", "i'll", "i'm", "i's", "i've", "in's", "isn't", "it'd", "it'll",
	"it's", "let's", "ma'am", "mayn't", "mightn't", "must've", "mustn't", "name's", "needn't", "ne'er",
	"o'clock", "oughtn't", "shan't", "she'd", "she'll", "she's", "should've", "shouldn't", "that'd", "that'll",
	"that's", "that've", "there'd", "there'll", "there're", "there's", "there've", "they'd", "they'll", "they're",
	"they've", "this'll", "wasn't", "we'd", "we'll", "we're", "we've", "weren't", "what'd", "what'll",
	"what're", "what's", "when's", "where'd", "where's", "which're", "which's", "who'd", "who'll", "who're",
	"who's", "who've", "whom's", "why'd", "why's", "will's", "won't", "would've", "wouldn't", "y'all",
	"ya'll", "you'd", "you'll", "you're", "you've", 
};


//合并英语中的'，比如what's、don't。
void FrontEngineInterface::FrontMergeEnglishThrow1(vector<pair<string, string>>& fvInParticiple)
{
	vector<pair<string, string>> tvOutResult;
	
	const size_t tInParticipleSize = fvInParticiple.size();
	for(size_t i=0; i<tInParticipleSize; i++) {
		if(i+2 < tInParticipleSize  &&  fvInParticiple[i+1].first == "\'" ) {
			string tMergeStr = fvInParticiple[i].first + "\'" + fvInParticiple[i+2].first;
			transform(tMergeStr.begin(), tMergeStr.end(), tMergeStr.begin(), ::tolower);		//大写转小写
			if( mcvEnThrowMerge.end() != find(mcvEnThrowMerge.begin(), mcvEnThrowMerge.end(), tMergeStr) ) {	//找到需要合并的词
				tMergeStr = fvInParticiple[i].first + "''" + fvInParticiple[i+2].first;			//对于sqlite查询'需要改成''
				tvOutResult.push_back( pair<string,string>(tMergeStr, "eng") );
				i += 2;		//偏移后面两位
				continue;
			}
		}
		//否则不需要合并
		tvOutResult.push_back( fvInParticiple[i] );
	}

	fvInParticiple = tvOutResult;
}


//合并 名词所有格's
void FrontEngineInterface::FrontMergeEnglishThrow2(vector<pair<string, string>>& fvInParticiple)
{
	vector<pair<string, string>> tvOutResult;
	
	const size_t tInParticipleSize = fvInParticiple.size();
	for(size_t i=0; i<tInParticipleSize; i++) {
		//如果是 单词+'+s的结构，则变成 单词+'s的结构。
		if(i+2 < tInParticipleSize  &&  fvInParticiple[i+1].first == "\'"  &&  fvInParticiple[i+2].first == "s" ) {
			tvOutResult.push_back( pair<string,string>(fvInParticiple[i].first, "eng") );
			tvOutResult.push_back( pair<string,string>("'s", "eng") );
			i += 2;		//偏移后面两位
			continue;
		}
		//否则不需要合并
		tvOutResult.push_back( fvInParticiple[i] );
	}

	fvInParticiple = tvOutResult;
}

